<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>CMS后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
</head>
<body th:fragment="body">
<!-- 预览框 -->
<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" id="modal-preview">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3>图片预览</h3>
            </div>
            <div class="modal-body">
                <img id="previewImg" class="img-responsive"/>
            </div>
        </div>
    </div>
</div>
<!-- 上传框 -->
<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" id="modal-upload">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3>图片上传</h3>
            </div>
            <form class="form-horizontal" action="/upload" method="post" enctype="multipart/form-data" target="hidden_frame" id="uploadForm">
                <div class="modal-body">
                    <iframe style="display: none" name="hidden_frame"></iframe>
                    <div class="input-group">
                        <div class="controls">
                            <input type="hidden" name="callbackId" id="callbackId"/>
                            <input type="file" name="uploadFile" id="uploadFile"/>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="uploadBtn">上传</button>
                    <button type="button" class="btn btn-success" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        // 图片预览
        $(".previewImg").on("click", function () {
            var imgUrl = $(this).attr("img-url");
            if (!imgUrl) {
                var id = $(this).attr("target-id");
                imgUrl = $("#" + id).val();
            }
            $("#previewImg").attr("src", imgUrl);
            $('#modal-preview').modal("show");
        });
        // 上传文件选择
        $(".uploadImg").on("click", function () {
            $("#uploadFile").val("");
            $("#callbackId").val($(this).attr("target-id"));
            $('#modal-upload').modal("show");
        });
        // 上传
        $("#uploadBtn").on("click", function () {
            if (!$("#uploadFile").val()) {
                alert("请选择文件！");
            } else {
                $("#uploadForm").submit();
                $(this).attr("disabled", "disabled");
            }
        });
    });

    // iframe中接收返回的图片url
    function callback(id, imgUrl) {
        $('#modal-upload').modal('hide');
        $("#" + id).val(imgUrl);
        $("#uploadBtn").removeAttr("disabled");
    }
</script>
</body>
</html>